<?php
    class StudentDormitoryAllocatingBLL{

        //为学生分配宿舍
        //未添加辅导员权限检查
        //未添加学生性别、年级、专业、班级验证
        //未验证床位数
        public function allocateStudentDorm($instructorID,$allocateList){
            $studentError=array();
            $sda=new StudentDormitoryAllocatingDAL();
            
            foreach($allocateList as $allocate){
                $status=$this->isStudentDormitory($allocate->student_id);
                if($status==1){
                    $studentError[]=$allocate->student_id;
                }else if($status==0){
                    $allocate->action=2;
                }
            }

            if(count($studentError)==0){
                $result=$sda->allocateStudentDorm($allocateList);
                if($result){
                    $message=array(
                        'code'=>'2001',
                        'message'=>'分配成功'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'分配失败'
                    );
                }
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'学生已分配宿舍',
                    'studentList'=>$studentError
                );
            }
            
            return $message;
        }

        //判断学生是否已分配宿舍
        public function isStudentDormitory($student_id){
            $status=0;
            $sda=new StudentDormitoryAllocatingDAL();
            $result=$sda->isStudentDormitory($student_id);
            if($result==false){
                $status=-1;
            }else{
                $status=$result['status'];
            }

            return $status;
        }

        //移除学生宿舍
        //未添加辅导员权限检查
        //未添加学生年级、专业、班级验证
        public function removeStudentDormitory($instructorID,$removeList){
            $sda=new StudentDormitoryAllocatingDAL();
            $result=$sda->removeStudentDormitory($removeList);
            if($result){
                $message=array(
                    'code'=>'2001',
                    'message'=>'移除成功'
                );
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'移除失败'
                );
            }

            return $message;
        }

        //获取学生列表
        public function getStudentList($allocate,$grade,$major,$class,$gender,$search,$page,$pageSize){
            $sda=new StudentDormitoryAllocatingDAL();

            $result=$sda->getStudentList($allocate,$grade,$major,$class,$gender,$search,$page*$pageSize,$pageSize);
            $result['count']=$this->getStudentListPage($result['count'],$pageSize);

            return $result;
        }
        //获取学生列表页数
        public function getStudentListPage($count,$pageSize){
            $pageMax=0;
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $pageMax=(int)($count/$pageSize);
                }else{
                    $pageMax=(int)($count/$pageSize)+1;
                }
            }
            
            return $pageMax;
        }

        //获取辅导员所管理的宿舍列表
        public function getDormitoryList($instructor,$grade,$major,$gender){
            $sda=new StudentDormitoryAllocatingDAL();
            $result=$sda->getDormitoryList($instructor,$grade,$major,$gender);

            return $result;
        }

        //获取宿舍已分配床位数量
        public function getAllocatedBedsNumber($apartmentID,$dormitoryID,$grade,$major){
            $sda=new StudentDormitoryAllocatingDAL();
            $result=$sda->getAllocatedBedsNumber($apartmentID,$dormitoryID,$grade,$major);

            return (int)$result['count'];
        }

        //判断辅导员是否符合所操作的年级、专业、班级
        public function isInstructor($instructorID,$grade,$major,$class){
            $flag=false;
            $ClassDormitoryManagementDAL=new ClassDormitoryManagementDAL();
            $result=$ClassDormitoryManagementDAL->isInstructor($instructorID,$grade,$major,$class);

            if(!$result==null){
                if($result['count']>0){
                    $flag=true;
                }
            }

            return $flag;
        }
    }
?>